************Veteran Crime Incarceration Rate***************
program define Inmate_counts, eclass

marksample touse

set more off


****Total observation of the sample 
count 
sca n_sample=r(N) 
sca li n_sample

***The bounded support for the outcome
sca yu=1
sca li yu 
sca yl=0
sca li yl

****The total number of inmates in each year in scaler
**Year 1979
sum svy_male_inmate if study==1979 
sca svy_male_inmate79=r(mean)
sca list svy_male_inmate79 

**Year 1986
sum svy_male_inmate if study==1986
sca svy_male_inmate86=r(mean)
sca list svy_male_inmate86 

**Year 1991: State
sum svy_male_inmate if study==1991
sca svy_male_inmate91_state=r(mean)
sca list svy_male_inmate91_state 

**Year 1991: Federal
sum svy_male_inmate if study==19912
sca svy_male_inmate91_fed=r(mean)
sca list svy_male_inmate91_fed 

**Aggregate population 
sca svy_male_inmate_all=svy_male_inmate79+svy_male_inmate86+svy_male_inmate91_state+svy_male_inmate91_fed
sca list svy_male_inmate_all

  **Total incarceration rate E[Y] in the population 
  gen cond=1 if Y==1 & Census==0 
  replace cond=0 if cond==. & Census==0
  
  *Year 1979:  
  sum cond if study==1979 [w=W]
  sca num_79=r(mean)*svy_male_inmate79
  sca li num_79
  
  *Year 1986:  
  sum cond if study==1986 [w=W]
  sca num_86=r(mean)*svy_male_inmate86
  sca li num_86
 
  *Year 1991 State:  
  sum cond if study==1991 [w=W]
  sca num_91_state=r(mean)*svy_male_inmate91_state
  sca li num_91_state
 

  *Year 1991 Federal 
  sum cond if study==19912 [w=W]
  sca num_91_fed= r(mean)*svy_male_inmate91_fed
  sca li num_91_fed
  
  drop cond
 
**E[Y|Z=1,T=0]
*Let cond stands for condition
gen cond=1 if T==0 & Z==1 & Y==1 & Census==0
replace cond=0 if cond==. & Census==0

*Year 1979:  
sum cond if study==1979 [w=W]
sca num_79_10=r(mean)*svy_male_inmate79
sca list num_79_10


*Year 1986:  
sum cond if study==1986 [w=W]
sca num_86_10=r(mean)*svy_male_inmate86
sca list num_86_10


*Year 1991 State:  
sum cond if study==1991 [w=W]
sca num_91_state_10=r(mean)*svy_male_inmate91_state
sca list num_91_state_10

*Year 1991 Federal 
sum cond if study==19912 [w=W]
sca num_91_fed_10=r(mean)*svy_male_inmate91_fed
sca list num_91_fed_10

drop cond 
 
**E[Y|Z=0,T=1]
*Let cond stands for condition
gen cond=1 if T==1 & Z==0 & Y==1 & Census==0
replace cond=0 if cond==. & Census==0

*Year 1979:  
sum cond if study==1979 [w=W]
sca num_79_01=r(mean)*svy_male_inmate79
sca li num_79_01


*Year 1986:  
sum cond if study==1986 [w=W]
sca num_86_01=r(mean)*svy_male_inmate86
sca li num_86_01


*Year 1991 State:  
sum cond if study==1991 [w=W]
sca num_91_state_01=r(mean)*svy_male_inmate91_state
sca li num_91_state_01

*Year 1991 Federal 
sum cond if study==19912 [w=W]
sca num_91_fed_01=r(mean)*svy_male_inmate91_fed
sca li num_91_fed_01

drop cond 

**E[Y|T=0,Z=0]
*Let cond stands for condition
gen cond=1 if T==0 & Z==0  & Y==1 & Census==0
replace cond=0 if cond==. & Census==0

*Year 1979:  
sum cond if study==1979 [w=W]
sca num_79_00=r(mean)*svy_male_inmate79
sca li num_79_00


*Year 1986:  
sum cond if study==1986 [w=W]
sca num_86_00=r(mean)*svy_male_inmate86
sca li num_86_00


*Year 1991 State:  
sum cond if study==1991 [w=W]
sca num_91_state_00=r(mean)*svy_male_inmate91_state
sca li num_91_state_00


*Year 1991 Federal 
sum cond if study==19912 [w=W]
sca num_91_fed_00=r(mean)*svy_male_inmate91_fed
sca li num_91_fed_00

drop cond 

**E[Y|Z=1,T=1]
*Let cond stands for condition
gen cond=1 if T==1 & Z==1  & Y==1 & Census==0
replace cond=0 if cond==. & Census==0

*Year 1979:  
sum cond if study==1979 [w=W]
sca num_79_11=r(mean)*svy_male_inmate79
sca li num_79_11

*Year 1986:  
sum cond if study==1986 [w=W]
sca num_86_11=r(mean)*svy_male_inmate86
sca li num_86_11

*Year 1991 State:  
sum cond if study==1991 [w=W]
sca num_91_state_11=r(mean)*svy_male_inmate91_state
sca li num_91_state_11

*Year 1991 Federal 
sum cond if study==19912 [w=W]
sca num_91_fed_11=r(mean)*svy_male_inmate91_fed
sca li num_91_fed_11

drop cond 


matrix I2=(n_sample, yu, yl, svy_male_inmate79, svy_male_inmate86, /// 
		   svy_male_inmate91_state, svy_male_inmate91_fed, svy_male_inmate_all, ///
		   num_79, num_86, num_91_state, num_91_fed, num_79_11, /// 
		   num_86_11, num_91_state_11, num_91_fed_11, num_79_10, num_86_10, num_91_state_10, /// 
		   num_91_fed_10, num_79_01, num_86_01, num_91_state_01, num_91_fed_01, /// 
		   num_79_00, num_86_00, num_91_state_00, num_91_fed_00 )
	
matrix colnames I2 = n_sample, yu, yl, svy_male_inmate79, svy_male_inmate86, /// 
		   svy_male_inmate91_state, svy_male_inmate91_fed, svy_male_inmate_all, ///
		   num_79, num_86, num_91_state, num_91_fed, num_79_11, /// 
		   num_86_11, num_91_state_11, num_91_fed_11, num_79_10, num_86_10, num_91_state_10, /// 
		   num_91_fed_10, num_79_01, num_86_01, num_91_state_01, num_91_fed_01, /// 
		   num_79_00, num_86_00, num_91_state_00, num_91_fed_00
	  
	    
	
mat li I2
		   
ereturn clear
ereturn post I2, esample (`touse') properties ("b")
ereturn local cmd "Inmate_counts"

end
